package com.dq.domain.sys.section;

import com.dq.vo.baseVo.VO;
import com.dq.domain.base.BaseEntity;
import com.dq.domain.sys.menu.SysMenu;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 部门表
 * @author cf
 */
@Data
@Entity
@Table(indexes = {@Index(columnList = "sectionCode")})
@ApiModel(value = "部门表")
public class SysSection extends BaseEntity implements VO.ToVO {
    @ManyToOne
    @ApiModelProperty(value = "父级", hidden = true)
    protected SysSection parent;
    @ApiModelProperty(value = "祖级列表")
//    @NotNull
    protected String ancestors;

    @ApiModelProperty(value = "编号")
    @Column(unique = true)
//    @NotBlank(message = "编号不能为空")
    protected String sectionCode;

    @ApiModelProperty(value = "简称")
    @NotBlank(message = "部门简称不能为空")
//    @Column(unique = true)
    protected String simpleName;
    @ApiModelProperty(value = "全称")
    @NotBlank(message = "部门全称不能为空")
    protected String fullName;

    @ApiModelProperty(value = "描述")
    protected String description;

    @ApiModelProperty(value = "排序")
    @NotNull(message = "排序不能为空")
    protected Integer sort;
    @ApiModelProperty(value = "状态")
    @NotNull(message = "状态不能为空")
    protected SysMenu.Status status;

    /** 非实体字段 */
    @Transient
    protected Long parentId;
    @ApiModelProperty("角色id集合")
    @Transient
    protected List<Long> roleIds;

    public SysSection(String sectionCode, String simpleName, String fullName, Integer sort, SysMenu.Status status, String ancestors) {
        this.sectionCode = sectionCode;
        this.simpleName = simpleName;
        this.fullName = fullName;
        this.sort = sort;
        this.status = status;
        this.ancestors = ancestors;
    }
    public SysSection() {
    }

    @Data
    @ApiModel(value = "部门表视图")
    public static class Vo implements VO {
        @ApiModelProperty(value = "上级部门")
        private Long parentId;
        @ApiModelProperty(value = "上级部门")
        private String parentSectionCode;
        @ApiModelProperty(value = "上级部门")
        private String parentSimpleName;
        @ApiModelProperty(value = "上级部门")
        private String parentFullName;

        @ApiModelProperty(value = "部门")
        private Long id;
        @ApiModelProperty(value = "部门")
        private String sectionCode;
        @ApiModelProperty(value = "部门")
        private String simpleName;
        @ApiModelProperty(value = "部门")
        private String fullName;

        @ApiModelProperty(value = "描述")
        private String description;

        @ApiModelProperty(value = "排序")
        private Integer sort;
        @ApiModelProperty("状态")
        private String statusEn;
        @ApiModelProperty("状态")
        private String statusCn;

        @ApiModelProperty("创建时间")
        protected String createTime;
        @ApiModelProperty("修改时间")
        protected String lastModifyTime;
        @ApiModelProperty("创建者")
        protected String createUserName;
        @ApiModelProperty("修改人")
        protected String updateUserName;

        /** set 赋值 */
        @ApiModelProperty("角色id集合")
        protected List<Long> roleIds = new ArrayList<>();

        private List<SysSection.Vo> children = new ArrayList<>();
    }
    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.parentId = this.getParent() == null ? 0 : this.getParent().getId();
        vo.parentSectionCode = this.getParent() == null ? "" : this.getParent().getSectionCode();
        vo.parentSimpleName = this.getParent() == null ? "" : this.getParent().getSimpleName();
        vo.parentFullName = this.getParent() == null ? "" : this.getParent().getFullName();

        vo.id = this.id;
        vo.sectionCode = this.getSectionCode();
        vo.simpleName = this.getSimpleName();
        vo.fullName = this.getFullName();

        vo.description = this.getDescription();
        vo.sort = this.getSort();
        vo.statusEn = this.status == null ? null : this.status.toString();
        vo.statusCn = this.status == null ? null : this.status.name;

        vo.createTime = fmt(this.getCreateTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.lastModifyTime = fmt(this.getLastModifyTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS);
        vo.createUserName = this.getCreateUser() == null ? "" : this.getCreateUser().getUserRealcName();
        vo.updateUserName = this.getUpdateUser() == null ? "" : this.getUpdateUser().getUserRealcName();

        return vo;
    }
}
